home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue61 / Contract / SimpleAssertFrm.pas < prev   
Encoding:
Pascal/Delphi Source File  |  2000-08-07  |  1.3 KB  |  68 lines

  1. unit SimpleAssertFrm;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     edtValue: TEdit;
  12.     btnCalc: TButton;
  13.     edtResult: TEdit;
  14.     lblEquals: TLabel;
  15.     procedure btnCalcClick(Sender: TObject);
  16.   private
  17.       procedure AssertValue(AValue: Extended);
  18.         function CalcSquareRoot(AValue: Extended): Extended;
  19.   public
  20.     { Public declarations }
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.DFM}
  29.  
  30.  
  31. { TForm1 }
  32.  
  33. function TForm1.CalcSquareRoot(AValue: Extended): Extended;
  34. begin
  35.     AssertValue(AValue);
  36. //    Assert(AValue >= 0,'Pre-Condition failed: value must be >= 0');
  37.   Result := Sqrt(AValue);
  38. end;
  39.  
  40. procedure TForm1.btnCalcClick(Sender: TObject);
  41. var
  42.     Value: Extended;
  43. begin
  44.  
  45.     try
  46.       Value := StrToFloat(edtValue.Text);
  47.     except
  48.       on EConvertError do begin
  49.         ShowMessage('Please enter a numerical value');
  50.         Exit;
  51.     end;
  52.   end;
  53.  
  54.   if Value < 0 then begin
  55.       ShowMessage('Square Root for a negative number is not defined!');
  56.   end else begin
  57.       edtResult.Text := FloatToStr(CalcSquareRoot(Value));
  58.   end;
  59.  
  60. end;
  61.  
  62. procedure TForm1.AssertValue(AValue: Extended);
  63. begin
  64.     Assert(AValue >= 0,'Pre-Condition failed: value must be >= 0');
  65. end;
  66.  
  67. end.
  68.